#!/bin/bash
########################################
#  This file is part of the open-eBackup project.
# Copyright (c) 2024 Huawei Technologies Co.,Ltd.
#
# open-eBackup is licensed under MPL v2.
# You can use this software according to the terms and conditions of the MPL v2.
# You may obtain a copy of MPL v2 at:
#
#          https://www.mozilla.org/en-US/MPL/2.0
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the MPL v2 for more details.
########################################

set -eux
set -o pipefail 
set +e

install_dir=$1
[ ! -d "${install_dir}" ] && rm -rf ${install_dir}
mkdir -p ${install_dir}

##调整yml文件的格式
cat /systemtools/var/tools.yml |sed 's/[ ]*$//g'>/systemtools/tools_sed.yml
sed -i 's/   -/      -/g' /systemtools/tools_sed.yml
sed -i 's/-  toolName/  - toolName/g' /systemtools/tools_sed.yml
sed -i 's/   packages/    packages/g' /systemtools/tools_sed.yml

chmod +x /systemtools/common/config.sh

##取出系统工具的包名
packages_array=($(bash /systemtools/common/config.sh -f /systemtools/tools_sed.yml| awk -F \" '{print $2}' | sed '1d'))

##判断OS的类型
system_name=$(bash /systemtools/common/config.sh -f /systemtools/tools_sed.yml|awk -F "toolName=" '{print $2}' |sed 's/"$//' | sed 's/"//' | sed '/^$/d')

suse_install() {
	for data in ${packages_array[@]};do
	   zypper install -y  $data
	   if [ $? -ne 0 ];then
		    echo "systemtools $data install Error, please check mirror,and call administrator"
		    exit 1
		fi
	done
}

euler_install() {
	for data in ${packages_array[@]};do
	    yum -y install $data
	    if [ $? -ne 0 ];then
		    echo "systemtools $data install Error, please check mirror,and call administrator"
		    exit 1
		fi
		if [ -f "/etc/yum.repos.d/openEuler.repo" ];then
		    rm -f /etc/yum.repos.d/openEuler.repo
		fi
	done
}
centos_install() {
	for data in ${packages_array[@]};do
	    yum -y install $data
	    if [ $? -ne 0 ];then
		    echo "systemtools $data install Error, please check mirror,and call administrator"
		    exit 1
		fi
	done
}
kylin_install() {
	for data in ${packages_array[@]};do
	    yum -y install $data
	    if [ $? -ne 0 ];then
		    echo "systemtools $data install Error, please check mirror,and call administrator"
		    exit 1
		fi
	done
}

ubuntu_install() {
	for data in ${packages_array[@]};do
	    apt -y install $data
	    if [ $? -ne 0 ];then
		    echo "systemtools $data install Error, please check mirror,and call administrator"
		    exit 1
	    fi
	done
}

##遍历下载安装
case "$system_name" in	
	Ubuntu)
		 ubuntu_install
	     echo "ubuntu systemtools install successful"
	;;
	EulerOS)
		#rpm --rebuilddb
		euler_install
		echo "EulerOS systemtools install sucessful"
	;;
	CentOS)
		rpm --rebuilddb
		centos_install
		echo "CentOS systemtools install sucessful"
	;;
	Kylin)
		rpm --rebuilddb
		kylin_install
		echo "Kylin systemtools install sucessful"
	;;
	SUSE)
		suse_install
		echo "SUSE systemtools install successful"
	;;
	*)
		echo "system tools is error ,please ensure your base image"
		exit 1
	;;
esac
